package org.dllwh.mongodb;

import com.mongodb.client.MongoCollection;
import jakarta.annotation.Resource;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName CollectionTest
 * @Description 集合操作测试类
 * @Date 2025-05-14 10:41
 * @版本 V 1.0.1
 * @since JDK 17
 */
@SpringBootTest
public class CollectionTest {
    @Resource
    private MongoTemplate mongoTemplate;
    private final String collectionName = "CollectionTest";

    @Test
    public void createCollectionTest() {
        if (mongoTemplate.collectionExists(collectionName)) {
            mongoTemplate.dropCollection(collectionName);
        }
        mongoTemplate.createCollection(collectionName);
    }

    @Test
    public void getCollectionTest() {
        MongoCollection<Document> mongoCollection = mongoTemplate.getCollection(collectionName);
        mongoCollection.countDocuments();
    }

    @Test
    public void getCollectionNamesTest() {
        mongoTemplate.getCollectionNames().forEach(System.out::println);
    }
}
